var order_p_buss_no,order_p_order_no,order_p_order_no_sys,order_p_order_status,order_p_product_no,order_p_print;
var del_id,del_order_no;
$(function(){
	queryClass();
	page.bind();
});
var page = { //页面总变量
	bind:function(){
		$("#search").unbind("click");
		$("#search").bind("click.my",function(){
			order_p_order_no=$("#order_p_order_no").val();
			order_p_order_status=$("#order_p_order_status").val();
			order_p_order_no_sys=$("#order_p_order_no_sys").val();
			order_p_product_no=$("#order_p_product_no").val();
			order_p_print=$("#order_p_print").val();
			order_p_buss_no=$("#order_p_buss_no").val();
			queryClass();
		})
		$(window.parent.document).find("span[data-href='orderywdlist.html']").parent().unbind('click.my');
		$(window.parent.document).find("span[data-href='orderywdlist.html']").parent().bind('click.my',function(){
			reloadData();
		})
		$("#delete_order_sure").unbind("click.my");
		$("#delete_order_sure").bind("click.my",function(){
			page.deletesure();
		})
	},
	seeAndPrint:function(self){//查看权限
		var order_no=$(self).attr("order_no");
		var template_id=$(self).attr("template_id");
		if(template_id==0){
			alert("该订单没有模版,不可编辑");
			return;
		}
		$.ajax({
	          type: 'POST',
	          url: "createOrderBusiness/getTemplateById",
	          data: {"id":template_id},
	          success: function(data){
	            console.info(data);
	            localStorage.setItem("temp_info_select",data.data.temp_info);
	            localStorage.setItem("temp_info_table_source_arr",data.data.table_source_arr);
	            localStorage.setItem("temp_info_table_source_arr_str",data.data.table_source_arr_str);
	            localStorage.setItem("table_source_line_total",data.data.table_source_line_total);
	            localStorage.setItem("temp_info_table_source_arr_for_two",data.data.table_source_arr_for_two);
	            localStorage.setItem("temp_info_table_source_arr_str_for_two",data.data.table_source_arr_str_for_two);
	            localStorage.setItem("table_source_line_total_for_two",data.data.table_source_line_total_for_two);
	            localStorage.setItem("template_id",template_id);
	            localStorage.setItem("template_name_old",data.data.temp_name);
	            $(window.parent.document).find("[data-href='./html/createOrder/orderSee.html']")[0].click();
	    		$(window.parent.document).find("iframe[src='./html/createOrder/orderSee.html']")[0].src="./html/createOrder/template.html?order_no="+order_no;
	    		page.hideFunforSee(order_no);
	          }
	       });
	},
	seeYwd:function(self){//查看权限
		var order_p_buss_no=$(self).attr("order_p_buss_no");
		$(window.parent.document).find("[data-href='./html/createOrder/orderSee.html']")[0].click();
	    $(window.parent.document).find("iframe[src='./html/createOrder/orderSee.html']")[0].src="./html/createOrder/index_x_ywd_list.html?order_p_buss_no="+order_p_buss_no;
	},
	hideFunforSee:function(order_no){
		var i=0;
		var Interval_i = setInterval (function(){
			console.info($("[src='./html/createOrder/template.html?order_no="+order_no+"']",parent.document).length);
			$("[src='./html/createOrder/template.html?order_no="+order_no+"']",parent.document).contents().find("#printArea").show();
			$("[src='./html/createOrder/template.html?order_no="+order_no+"']",parent.document).contents().find("#printArea").siblings().hide();//所有功能屏蔽
			$("[src='./html/createOrder/template.html?order_no="+order_no+"']",parent.document).contents().find("#formsubmitAsNewOrder").show();//保存新订单
			$("[src='./html/createOrder/template.html?order_no="+order_no+"']",parent.document).contents().find("#menubutton_index").show();//小工具
			if(i>5){
				clearInterval(Interval_i);
			}
			i++;
		},1000);
	},
	edit:function(self){//编辑
		var order_no=$(self).attr("order_no");
		var template_id=$(self).attr("template_id");
		if(template_id==0){
			alert("该订单没有模版,不可编辑");
			return;
		}
		$.ajax({
	          type: 'POST',
	          url: "createOrderBusiness/getTemplateById",
	          data: {"id":template_id},
	          success: function(data){
	            console.info(data);
	            localStorage.setItem("temp_info_select",data.data.temp_info);
	            localStorage.setItem("temp_info_table_source_arr",data.data.table_source_arr);
	            localStorage.setItem("temp_info_table_source_arr_str",data.data.table_source_arr_str);
	            localStorage.setItem("table_source_line_total",data.data.table_source_line_total);
	            localStorage.setItem("temp_info_table_source_arr_for_two",data.data.table_source_arr_for_two);
	            localStorage.setItem("temp_info_table_source_arr_str_for_two",data.data.table_source_arr_str_for_two);
	            localStorage.setItem("table_source_line_total_for_two",data.data.table_source_line_total_for_two);
	            localStorage.setItem("template_id",template_id);
	            localStorage.setItem("template_name_old",data.data.temp_name);
	            $(window.parent.document).find("[data-href='./html/createOrder/orderEdit.html']")[0].click();
	    		$(window.parent.document).find("iframe[src='./html/createOrder/orderEdit.html']")[0].src="./html/createOrder/template.html?order_no="+order_no;
	          }
	       });
	},
	audit:function(self){//审核页面 修改订单状态
		var order_no=$(self).attr("order_no");
		var template_id=$(self).attr("template_id");
		if(template_id==0){
			alert("该订单没有模版,不可编辑");
			return;
		}
		$.ajax({
	          type: 'POST',
	          url: "createOrderBusiness/getTemplateById",
	          data: {"id":template_id},
	          success: function(data){
	            console.info(data);
	            localStorage.setItem("temp_info_select",data.data.temp_info);
	            localStorage.setItem("temp_info_table_source_arr",data.data.table_source_arr);
	            localStorage.setItem("temp_info_table_source_arr_str",data.data.table_source_arr_str);
	            localStorage.setItem("table_source_line_total",data.data.table_source_line_total);
	            localStorage.setItem("temp_info_table_source_arr_for_two",data.data.table_source_arr_for_two);
	            localStorage.setItem("temp_info_table_source_arr_str_for_two",data.data.table_source_arr_str_for_two);
	            localStorage.setItem("table_source_line_total_for_two",data.data.table_source_line_total_for_two);
	            localStorage.setItem("template_id",template_id);
	            localStorage.setItem("template_name_old",data.data.temp_name);
	            $(window.parent.document).find("[data-href='./html/createOrder/orderAudit.html']")[0].click();
	    		$(window.parent.document).find("iframe[src='./html/createOrder/orderAudit.html']")[0].src="./html/createOrder/audit.html?order_no="+order_no;
	          }
	       });
	},
	deletedialog:function(self){//删除提示框
		$("#modal-demo").modal("show");
		del_order_no=$(self).attr("order_no");
		del_id=$(self).val();
	},deletesure:function(){//删除ajax
		$.ajax({
	          type: 'POST',
	          url: "createOrderBusiness/deleteOrderInfoPrimary",
	          data: {"id":del_id,"order_no":del_order_no},
	          success: function(data){
	        	  reloadData();
	          }
	       });
		$("#modal-demo").modal("hide");
	},applyedit:function(self){//申请修改订单信息
		var order_no=$(self).attr("order_no");
		var template_id=$(self).attr("template_id");
		$(window.parent.document).find("[data-href='./html/createOrder/orderApplyMainForApply.html']")[0].click();
		$(window.parent.document).find("iframe[src='./html/createOrder/orderApplyMainForApply.html']")[0].src="./html/createOrder/orderApplyMainForApply.html?template_id="+template_id+"&order_no="+order_no;
	},applyAudit:function(self){//审核订单申请信息
		var order_no=$(self).attr("order_no");
		var template_id=$(self).attr("template_id");
		var audit_apply_id=$(self).attr("audit_apply_id");
		if(audit_apply_id==0){
			alert("当前订单["+order_no+"]查询不到申请单编号");
			return;
		}
		$(window.parent.document).find("[data-href='./html/createOrder/orderApplyMainForSeeAndAudit.html']")[0].click();
		$(window.parent.document).find("iframe[src='./html/createOrder/orderApplyMainForSeeAndAudit.html']")[0].src="./html/createOrder/orderApplyMainForSeeAndAudit.html?template_id="+template_id+"&order_no="+order_no+"&audit_apply_id="+audit_apply_id;
	}
}
function reloadData(){
	var table = $('#attendanceClassTable').DataTable();
	table.draw(false);
}
//查询
function queryClass(){
	//避免多次绑定
	$('#attendanceClassTable tbody').unbind("click");
	ajaxUtil.dataTablesAjax("attendanceClassTable",10,"order/getOrderListForBusiness",
			{"order_p_buss_no":order_p_buss_no,"only_ywd":1,"order_p_print":order_p_print,"order_p_product_no":order_p_product_no,"order_p_order_no":order_p_order_no,"order_p_order_no_sys":order_p_order_no_sys,"order_p_order_status":order_p_order_status},[
			{"data":"order_p_order_status",render:function(data){
					  if(!data) return '计划';
					  if(data=="审核") return '审核成功';
					  return data;
			} },
			{"data":"order_p_company_order_no"},
			{"data":"order_p_product_no",render:function(data,type,row){
				if(data){return data;}else{
					return "";
				}
			}},
			{"data":"order_p_order_make_time",render:function(data,type,row){
				if(data){
					return '<span title="'+data+'">'+data.substring(0,10)+'</span>';
				}else{
					return "";
				}
			}},
			{"data":"order_p_version_no", render:function(data,type,row){
				if(data){return data;}else{
					return "";
				}
			}},
			{"data":"order_p_create", render:function(data,type,row){
				if(data){return data;}else{
					return "";
				}
			}},
			{"data":"order_p_buss_no", render:function(data,type,row){
				if(data){return data;}else{
					return "";
				}
			}},
			{"data":"order_p_print", render:function(data,type,row){
				if(data>0){
					return "<span style='color:green'>已打印</span>";
				}
				return "<span style='color:gray'>未打印</span>";
			}},
			{"data":"id",sWidth:"20%",render:function(data,type,row){
			   /*if(true){
				   return "<button class='edit-btn btn' type='button' value='"+data+"'>审核</button>";
			   }else{
				   return "<button class='edit-btn btn' type='button' value='"+data+"'>编辑</button>";
			   }*/
			   //console.info(row);
			   if(row.template_id!=0){
				   var str='';
				   var seeAndPrint=false;//查看权限
				   if(row.audit==1){//有审核权限
					   if(row.order_p_order_status!="审核"||true){//没审核     只有 订单审核权限
						   str+="<button id='btn_audit' order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' class='btn btn-link' onclick='page.audit(this)' type='button' value='"+data+"'>订单审核</button>";
					   }else{//审核成功 有查看权限
						   seeAndPrint=true;
					   }
				   }
				   if(row.edit==1){//有编辑权限
					   if(row.order_p_order_status=="审核"){//审核了 有查看权限 申请修改
						   seeAndPrint=true;
						   if(row.applyTotal<=row.auditTotal){//申请数小于审核数 可以修改
							   //str+="<button id='btn_audit' order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' class='btn btn-link' onclick='page.applyedit(this)' type='button' value='"+data+"'>申请修改</button>";
						   }
					   }
					   if(row.order_p_order_status!="审核"){//没有审核成功有编辑权限下 可以删除订单
						   str+="<button id='btn_edit'  order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' onclick='page.edit(this)' class='btn btn-link' type='button' value='"+data+"'>编辑</button>";
						   str+="<button id='btn_edit'  order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' onclick='page.deletedialog(this)' class='btn btn-link' type='button' value='"+data+"'>删除</button>";
					   }
				   }
				   if(seeAndPrint){
					   str+="<button id='btn_edit'  order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' onclick='page.seeAndPrint(this)' class='btn btn-link' type='button' value='"+data+"'>查看</button>";
				   }
				   if(row.applyTotal>row.auditTotal){//该订单有申请修改  且未审核 则出现更新审核权限 申请数大于审核数
					   str+="<button id='btn_audit' order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' audit_apply_id='"+row.audit_apply_id+"' class='btn btn-link' onclick='page.applyAudit(this)' type='button' value='"+data+"'>更新审核</button>";
				   }
				   if(!seeAndPrint&&""==str){//没有任何权限只能查看
					   str+="<button id='btn_edit'  order_no='"+row.order_p_order_no+"' template_id='"+row.template_id+"' onclick='page.seeAndPrint(this)' class='btn btn-link' type='button' value='"+data+"'>查看</button>";
				   }
				   //str+="<button id='btn_edit'  order_no='"+row.order_p_order_no+"' order_p_buss_no='"+row.order_p_buss_no+"' template_id='"+row.template_id+"' onclick='page.seeYwd(this)' class='btn btn-link' type='button' value='"+data+"'>查看业务单</button>";
				   return str;
			   }
				   
			   return '暂无操作';
			} }
	]);
	$('#attendanceClassTable').on( 'init.dt', function ( e, settings ) {
		layer.msg("查询完成",{icon:1});
	});
	
}

/*
$("iframe[src='./html/createOrder/index_x.html']")[0].contentWindow.basic_index.getTempForOrder('182','SC180308000010')*/




